const str1: string = '({[]})'  //true

function isValid(str: string) {
    let newStr = str.replace(' ', '')  //过滤空格
    // 奇数说明有不匹配的，直接返回false
    if (newStr.length % 2 !== 0) {
        return false
    }
    let arr: string[] = []
    for (let i = 0; i < newStr.length; i++) {
        // 匹配开始字符
        if (str[i] === '(' || str[i] === '{' || str[i] === '[') {
            arr.push(str[i])
        } else if (str[i] === ')' && str[arr.length - 1] === '(') {
            arr.pop()
        } else if (str[i] === '}' && str[arr.length - 1] === '{') {
            arr.pop()
        } else if (str[i] === ']' && str[arr.length - 1] === '[') {
            arr.pop()
        } else {
            return false
        }
    }
    return !arr.length
}
console.log(isValid(str1));//true
console.log(isValid('{[()}]')); //false

